import Vuex from 'vuex';
import $, { emitter } from '$';

export default new Vuex.Store({
  state: {
    paperInfo: {
      paperCharpts: [] as any[]
    },
    classType: 1
  },
  mutations: {
    set_paper_info(state, payload) {
      state.paperInfo = $.clone(payload);
      state.paperInfo.paperCharpts.reduce((num, item) => {
        item.questions.map(q => { num++; q.index = num });
        return num;
      }, 0)
      emitter.emit('test-paper-change');
    },
    set_paper_charpts(state, payload) {
      emitter.emit('test-paper-change');
      state.paperInfo.paperCharpts = $.clone(payload);
      console.log('test-paper-change', state.paperInfo.paperCharpts)
      state.paperInfo.paperCharpts.reduce((num, item) => {
        item.questions.map(q => { num++; q.index = num});
        return num;
      }, 0)
    },
    set_class_type(state, payload) {
      state.classType = payload
    }
  },
  getters: {
    paperCharpts: state => state.paperInfo.paperCharpts
  }
});
